#include "customwebpage.h"

CustomWebPage::CustomWebPage(QWebEngineProfile *profile, QObject *parent)
    : QWebEnginePage{profile, parent}
{

}


// 对url进行过滤拦截
bool CustomWebPage::acceptNavigationRequest(const QUrl &url, NavigationType type, bool isMainFrame)
{
    /**
     * 对url的拦截包括黑名单拦截和对url路径本身的存在的注入检测
     */
    std::string serverHost = ConfigManager::getInstance().getServerPath();
    std::string checkUrl = serverHost + "/urlCheck";

    QtHttpUtils utils;
    QJsonObject param;
    param.insert("url", url.toString());
    QJsonObject resp = utils.post(QUrl(QString(checkUrl.c_str())), param);
    qDebug() << "resp: " << resp;
    bool blackList = resp.value("blackList").toBool(false);
    bool xssInject = resp.value("xssInject").toBool(false);
    if(!blackList && !xssInject)return true;
    QString msg = "";
    if(blackList){
        msg += "目标站点可能是一个恶意站点！！！\n";
    }
    if(xssInject){
        msg += "检测到访问地址可能存在xss注入风险！！\n"
               "目标链接：" + url.toString();
    }

    CustomWarningDialog *customWarningDialog = new CustomWarningDialog();
    customWarningDialog->setContent(msg);
    int ret = customWarningDialog->exec();

    if(ret){
        return true;
    }
    return false;
}
